//
//  CallViewModel.m
//  EncryptionTPB
//
//  Created by LvJianfeng on 15/9/22.
//  Copyright © 2015年 coderjianfeng. All rights reserved.
//

#import "CallViewModel.h"
#import "AFNetworking.h"
#import "JFUtil.h"

@implementation CallViewModel
- (void)getBelongingPlaceWithNumber:(NSString*)phone delegate:(id<CallViewModelDelegate>)delegate;{
    if ([JFUtil replaceBlankSpace:phone].length<=0) {
        return;
    }
    BOOL isNumber = [JFUtil validateNumber:phone];
    if (!isNumber) {
        return;
    }
    NSString *url = [NSString stringWithFormat:@"http://a.apix.cn/apixlife/phone/phone?phone=%@",phone];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"38466a76d87d4f286a0ba057bb9f0a3d" forHTTPHeaderField:@"apix-key"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSDictionary *data = [responseObject objectForKey:@"data"];
        int errcode = [[responseObject objectForKey:@"error_code"] intValue];
        Phone *phone = [Phone new];
        phone.city = [data objectForKey:@"city"];
        phone.operatorString = [data objectForKey:@"operator"];
        phone.province = [data objectForKey:@"province"];
        phone.telephone = [data objectForKey:@"telephone"];
        [delegate getBelongingPlaceModel:phone errCode:errcode];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        [delegate getBelongingPlaceModel:nil errCode:1];
    }];
}
@end
